import time
from logging import exception
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait#导包

driver = webdriver.Edge()   #获取浏览器
driver.get("http://127.0.0.1/ranzhi/www/")   #打开网页
# time.sleep(1)
# 用户名密码
driver.find_element("xpath",'//*[@id="account"]').send_keys("admin")
driver.find_element("xpath",'//*[@id="password"]').send_keys("123456")
# sleep(2)
# 点击登录
driver.find_element("xpath",'//*[@id="submit"]').click()
# time.sleep(1)
WebDriverWait(driver,5,0.5).until(expected_conditions.presence_of_element_located(("id", "desktop")))
# 点击后台管理
driver.find_element("xpath",'//*[@id="s-menu-superadmin"]/button/i').click()
# sleep(2)
WebDriverWait(driver,5,0.5).until(expected_conditions.presence_of_element_located(("id", "iframe-superadmin")))
# 切换到iframe
# 定位到iframe
iframe = driver.find_element("id","iframe-superadmin")
driver.switch_to.frame(iframe)
# sleep(2)
# 点击添加成员
driver.find_element("class name","shortcut.user").click()
# sleep(1)
WebDriverWait(driver,5,0.5).until(expected_conditions.presence_of_element_located(("id","account")))
#定位用户名并输入
driver.find_element("id",'account').send_keys("ddmin")
# sleep(1)
#定位真实姓名并输入
driver.find_element("id",'realname').send_keys("李四")
# sleep(1)
#选择性别男并选择
driver.find_element("id",'genderm').click()
# sleep(1)
#部门下来框
role1 = driver.find_element("id","dept")
s1 = Select(role1)     #强转为select对象
ls = s1.options   # 获取所有的下拉选项的对象
listtext1 = []
for i in ls:
    listtext1.append(i.text)
print(listtext1)
s1.select_by_visible_text(listtext1[3])   # 选中
# time.sleep(2)
# 角色下拉框
role = driver.find_element("id","role")
s = Select(role)     #强转为select对象
ls = s.options   # 获取所有的下拉选项的对象
listtext = []
for i in ls:
    listtext.append(i.text)
print(listtext)
s.select_by_visible_text(listtext[3])   # 选中
# time.sleep(2)
#定位密码并输入
driver.find_element("id",'password1').send_keys("123456")
# sleep(1)
#定位重新输入密码
driver.find_element("id",'password2').send_keys("123456")
# sleep(1)
#定位邮箱并输入
driver.find_element("id","email").send_keys("3234567891@qq.com")
# sleep(1)
#定位保存并点击
driver.find_element("id","submit").click()
# sleep(20)
WebDriverWait(driver,5,0.5).until(expected_conditions.presence_of_element_located(("class name", "clearfix")))
#截屏
timestr = time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime())
driver.get_screenshot_as_file(f"d:\\baidu{timestr}.png")
time.sleep(3)
#回退
driver.back()
sleep(2)
#签退
driver.find_element("class name","sign.signout").click()
sleep(3)